package cn.ococci.dm.Main.Fragment;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.wifi.ScanResult;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import cn.ococci.dm.Base.BaseApp;
import cn.ococci.dm.Base.BaseFragment;
import cn.ococci.dm.Main.Adapter.Base.AbNormalAdapter;
import cn.ococci.dm.Main.Adapter.Base.BaseViewHolder;
import cn.ococci.dm.Main.Adapter.Base.IMultiItemType;
import cn.ococci.dm.Main.DataBuilder;
import cn.ococci.dm.Main.DoorBiz;
import cn.ococci.dm.Main.Model.DoorModel;
import cn.ococci.dm.Main.Model.DoorUnitModel;
import cn.ococci.dm.Main.Tools.DateTool;
import cn.ococci.dm.Main.Tools.DbTool;
import cn.ococci.dm.Main.Tools.DialogTool;
import cn.ococci.dm.Main.Tools.FilterTool;
import cn.ococci.dm.Main.Tools.ImageTool;
import cn.ococci.dm.Main.Tools.StringTool;
import cn.ococci.dm.Main.Tools.ThreadTool;
import cn.ococci.dm.Main.Tools.ToastTool;
import cn.ococci.dm.Main.UserBiz;
import cn.ococci.dm.Main.View.Dialog.DialogAdapter;
import cn.ococci.dm.Network.HttpCallback;
import cn.ococci_dm.R;

/**
 * Created by cyan on 15/12/29.
 *  主界面门禁显示和操作列表
 */
public class HomeFragment extends BaseFragment {

    private ListView doorManagerLv;

    private ArrayList<DoorModel> arrayDoor;
    private List<ScanResult> scanResultsList;

    private AbNormalAdapter<DoorModel> abNormalAdapter;

    private DoorModel curDoor;


    @Override
    protected int getResId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(View parent) {

        doorManagerLv = (ListView) parent.findViewById(R.id.doorManagerLv);

    }

    @Override
    protected void initData() {
        arrayDoor = new ArrayList<>();
        FilterTool.addHeaderData(arrayDoor);

        DoorBiz.getDoorList(new HttpCallback<DoorModel>() {
            @Override
            public void onSuccessList(List<DoorModel> data) {
                DataBuilder.addDataWithScanWiFi(data,arrayDoor);
                ThreadTool.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initAdapter();
                    }
                });

            }
        });
    }

    @Override
    public void initBackData() {
        abNormalAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initAdapter() {

        IMultiItemType<DoorModel> type = new IMultiItemType<DoorModel>() {
            @Override
            public int getLayoutId(int position, DoorModel doorModel) {

                if (position == 0){
                    return R.layout.fragment_home_header_item;
                }
                return R.layout.fragment_home_list_item;
            }

            @Override
            public int getViewTypeCount() {
                return 2;
            }

            @Override
            public int getItemViewType(int position, DoorModel doorModel) {
                return position == 0? 0:1;
            }

        };

        abNormalAdapter = new AbNormalAdapter<DoorModel>(getActivity(),arrayDoor,type) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, final DoorModel door, final int pos) {

                if (pos != 0){
                    initHolderView(baseViewHolder, door);
                }

            }
        };
        doorManagerLv.setAdapter(abNormalAdapter);

    }

    @Override
    protected void initListener() {
        doorManagerLv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (DateTool.delayTime()){
                    getMainActivity().initScanWiFi();
                }

                return false;
            }
        });
    }

    private void initHolderView(BaseViewHolder baseViewHolder, final DoorModel door) {

        boolean isScan = StringTool.isEmpty(door.getDoorName());

        baseViewHolder.setTextView(R.id.doorNameTv,door.getDoorName(),!isScan);
        baseViewHolder.setTextView(R.id.wiFiNameTv,door.getWifiName());

        baseViewHolder.setTextView(R.id.managerTv, isScan?"新增":"修改", true, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                curDoor = door;
                ModifyDoorFragment modifyDoorFragment = new ModifyDoorFragment();
                modifyDoorFragment.setDoor(curDoor);

                if (StringTool.hasData(scanResultsList)){
                    modifyDoorFragment.refreshListView(scanResultsList);
                }
                turnTo(modifyDoorFragment);
            }
        });

        // synImageView

        if(!isScan && (door.getSyn() != 0)){

            Bitmap bitmap = ImageTool.replaceColorPix(R.mipmap.refresh_btn, Color.parseColor("#3F51B5"));

            baseViewHolder.setImageView(R.id.synImg, bitmap,true, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    curDoor = door;
                    synDoorUnitToCloud();
                }
            });

        }else{

            baseViewHolder.setImageView(R.id.synImg,false);
        }

    }


    private void synDoorUnitToCloud(){

        List<DoorUnitModel> tmpList = DbTool.findDoorUnitListWithNeedSyn(curDoor.getDoorId());

        if (!StringTool.hasData(tmpList)){
            ToastTool.show(R.string.main_door_syn_no_data);
            return;
        }

        DoorBiz.setDoorPermission(false, curDoor.getDoorId(), tmpList, new HttpCallback() {
            @Override
            public void onSuccess(Object data) {
                ToastTool.showOnUiThread(R.string.main_door_syn_success);
            }
        });
    }

    // MainActivity调用
    public void refreshListView(List<ScanResult> list){

        this.scanResultsList =  list;
        if (FilterTool.filterWiFiDataWithCanRefresh(arrayDoor,FilterTool.retrievalScanResult(scanResultsList))){
            initAdapter();
        }
    }



    @Override
    public String getCaption() {
        return BaseApp.findResIdDecoderString(R.string.main_fragment_title);
    }

    @Override
    public String getBackCaption() {
        return "";
    }


    @Override
    public String getRightText() {
        return BaseApp.findResIdDecoderString(R.string.logout);
    }

    @Override
    public void onRightTextClick() {

        DialogTool.createMaterialDialog(getActivity(), new DialogAdapter() {
            @Override
            public boolean confirm() {

                UserBiz.logout();
                return super.confirm();
            }

            @Override
            public CharSequence getMsg() {
                return BaseApp.findResIdDecoderString(R.string.me_logout_msg);
            }

            @Override
            public int getCaptionResId() {
                return R.string.app_logout;
            }
        });

    }


}
